package com.powerbridge.bls.cod;

import com.powerbridge.bls.cod.service.DecryptXmlService;
import com.powerbridge.bls.cod.service.EncryptXmlService;
import org.apache.log4j.Logger;

import java.util.ResourceBundle;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Description:程序主入口
 * @Author: xiegelin
 * @Date: 2018/12/31 14:33
 */
public class MainClass {
    private static Logger log=Logger.getLogger(MainClass.class);

    private static String beforeEncryptPath="";//报文加密前的路径
    private static String afterEncryptPath="";//报文加密后的路径
    private static String beforeDecryptPath="";//报文解密前的路径
    private static String afterDecryptPath="";//报文解密后的路径
    private static int intervalTimeMillis=0;//定时任务间隔时间
    static{
        ResourceBundle rb = ResourceBundle.getBundle("config");
        beforeEncryptPath=rb.getString("beforeEncryptPath");
        afterEncryptPath=rb.getString("afterEncryptPath");
        beforeDecryptPath=rb.getString("beforeDecryptPath");
        afterDecryptPath=rb.getString("afterDecryptPath");
        intervalTimeMillis= Integer.parseInt(rb.getString("intervalTimeMillis"));
    }

    public static void main(String[] args){
        log.info("您的报文加密前的路径为:"+beforeEncryptPath);
        log.info("您的报文加密后的路径为:"+afterEncryptPath);
        log.info("您的报文解密前的路径为:"+beforeDecryptPath);
        log.info("您的报文解密后的路径为:"+afterDecryptPath);
        log.info("您的定时任务时间间隔为:"+intervalTimeMillis+"毫秒");
        log.info("报文加工小工具开始启动...");
        //开启2个线程，加密解密各一个线程。
        Runnable encodeRunnable=new Runnable() {
            public void run() {
                //报文加密
                EncryptXmlService encryptXmlService=new EncryptXmlService(
                        beforeEncryptPath,afterEncryptPath);
                encryptXmlService.doEncrypt();
            }
        };
        Runnable decodeRunnable=new Runnable() {
            public void run() {
                //报文解密
                DecryptXmlService decryptXmlService=new DecryptXmlService(
                        beforeDecryptPath,afterDecryptPath);
                decryptXmlService.doDecrypt();
            }
        };
        ScheduledExecutorService executorService1= Executors.newSingleThreadScheduledExecutor();
        //scheduleAtFixedRate:按固定频率周期执行，如果任务执行时间超过周期，不会并发执行，阻塞式，线程安全。
        executorService1.scheduleAtFixedRate(encodeRunnable,1000,intervalTimeMillis, TimeUnit.MILLISECONDS);
        ScheduledExecutorService executorService2= Executors.newSingleThreadScheduledExecutor();
        executorService2.scheduleAtFixedRate(decodeRunnable,1000,intervalTimeMillis,TimeUnit.MILLISECONDS);
    }
}
